package edcom.server;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletCookieHandler {
	public Cookie getCookie(String name, HttpServletRequest request){
		Cookie[] set = request.getCookies();
		if(set != null) {
			for(int i = 0; i < set.length; i++) {
				if(set[i].getName().equals(name)) return set[i];
			}
		}
		return null;
	}
	
	public String getCookieValue(String name, HttpServletRequest request) {
		Cookie var = this.getCookie(name, request);
		if(var != null) {
			return var.getValue();
		}
		return null;
	}
	
	public boolean setCookie(String name, String value, HttpServletResponse response) {
		try {
			Cookie var = new Cookie(name, value);
			response.addCookie(var);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean setCookie(String name, String value, String path, HttpServletResponse response) {
		try {
			Cookie var = new Cookie(name, value);
			var.setPath(path);
			response.addCookie(var);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}

